package com.norma.netty.study.netty.heartbeat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @author zhangchaofeng
 * @date 2020/12/30
 * @description 心跳客户端
 */
public class NettyHeartbeatClient {

    public static void main(String[] args) {
        EventLoopGroup group = new NioEventLoopGroup();
        NettyHeartbeatClientHandler nettyHeartbeatClientHandler = new NettyHeartbeatClientHandler();
        Bootstrap b;
        try {
            b = new Bootstrap().group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new ChannelInboundHandlerAdapter(){
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    System.out.println("--");
                                }
                            });
                            pipeline.addLast(new IdleStateHandler(3, 4, 5));
                            pipeline.addLast(nettyHeartbeatClientHandler);
                        }
                    });
            System.out.println("-->开始连接");
            b.connect("127.0.0.1", 8888).sync();
        } catch (InterruptedException e) {
            group.shutdownGracefully();
        }

    }

}
